<template>
  <div class="system-container">
    <vab-query-form>
      <el-form :inline="true" :model="queryForm" @submit.native.prevent>
        <el-form-item class="pr8">
          <el-input
            v-model.trim="queryForm.APPLY_NUM"
            class="wd150"
            clearable
            placeholder="申请单号"
          />
        </el-form-item>
        <el-form-item class="pr8">
          <el-select
            v-model="queryForm.BLOOD_PURPOSE"
            class="wd150"
            clearable
            filterable
            placeholder="输血目的"
          >
            <el-option
              v-for="item in bloodPurposeList"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="pr8">
          <el-input
            v-model.trim="queryForm.MATCH_SHEET_NUM"
            class="wd150"
            clearable
            placeholder="配血单号"
          />
        </el-form-item>
        <el-form-item class="pr8">
          <el-input
            v-model.trim="queryForm.PATIENT_ID"
            class="wd150"
            clearable
            placeholder="门诊号"
          />
        </el-form-item>
        <el-form-item class="pr8">
          <el-input
            v-model.trim="queryForm.PAT_NAME"
            class="wd150"
            clearable
            placeholder="患者姓名"
          />
        </el-form-item>
        <el-form-item class="pr8">
          <el-date-picker
            v-model="queryForm.BEGIN_DATE"
            class="wd150"
            placeholder="开始日期"
            type="date"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="queryForm.END_DATE"
            class="wd150"
            placeholder="结束日期"
            type="date"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="queryData">
            查询
          </el-button>
          <el-button icon="el-icon-close" type="success" @click="handleInvalid">
            作废
          </el-button>
          <el-button
            icon="el-icon-refresh"
            type="warning"
            @click="handleRefresh"
          >
            刷新
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :row-class-name="tableRowClassName"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="配血单号"
        prop="MATCH_SHEET_NUM"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="申请单号"
        prop="APPLY_NUM"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="输血目的"
        prop="BLOOD_PURPOSE"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="科室"
        prop="DEPT_NAME"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        label="储血号"
        prop="BLOOD_ID"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="配血试管号"
        prop="BLOOD_ID_UP"
        show-overflow-tooltip
        width="90"
      />
      <el-table-column
        align="center"
        label="受血者"
        prop="PAT_NAME"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="门诊号"
        prop="PATIENT_ID"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="住院号"
        prop="INP_NO"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="献血者"
        prop="NAME"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="血液成分"
        prop="BLOOD_TYPE_NAME"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        label="受血者血型/RHD"
        prop="PAT_BLOOD_TYPE"
        show-overflow-tooltip
        width="80"
      >
        <template #default="{ row }">
          {{ `${row.PAT_BLOOD_TYPE} / ${row.PAT_RH}` }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="献血者血型/RHD"
        prop="BLOOD_GROUP"
        show-overflow-tooltip
        width="80"
      >
        <template #default="{ row }">
          {{ `${row.BLOOD_GROUP} / ${row.RH}` }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="储血量(U)"
        prop="BLOOD_CAPACITY"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="配血结果"
        prop="MATCH_RESULT"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="配血者"
        prop="MATCH_OPERATOR"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="配血时间"
        prop="MATCH_DATE"
        show-overflow-tooltip
        width="140"
      >
        <template #header>
          <div class="flex flex-column">
            <span>配血时间</span>
            <span>用血时间</span>
          </div>
        </template>
        <template #default="{ row }">
          <div class="flex flex-column">
            <span>{{ getDate(row.MATCH_DATE) }}</span>
            <span>{{ getDate(row.TRANS_DATE) }}</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  import {
    PLAK0017 as getList,
    PLAK0018 as invalidBloodMatches,
  } from '@/api/client'
  import { mapGetters } from 'vuex'
  import { filterList } from '@/utils'
  import { bloodPurposeList } from '@/utils/constant'
  import {
    arraySort,
    getMatchTransClass,
    removeEmptyField,
  } from '@/utils/index'

  export default {
    name: 'Invalid',
    props: {
      activeName: {
        type: String,
        default() {
          return 'second'
        },
      },
    },
    data() {
      return {
        bloodPurposeList,
        list: [],
        listLoading: false,
        selectRows: '',
        queryForm: {
          MATCH_SHEET_NUM: '',
          APPLY_NUM: '',
          PATIENT_ID: '',
          PATIENT_NAME: '',
          BEGIN_DATE: '',
          END_DATE: '',
          BEGIN_TIME1: '',
          END_TIME1: '',
          MATCH_DATE: '',
          BLOOD_PURPOSE: '',
        },
      }
    },
    computed: {
      ...mapGetters({
        bloodMatchTypePid: 'initialData/bloodMatchTypePid',
      }),
    },
    watch: {
      // 'queryForm.MATCH_DATE': {
      //   handler(newVal) {
      //     if (newVal) {
      //       this.queryForm.BEGIN_DATE = newVal[0]
      //       this.queryForm.END_DATE = newVal[1]
      //     }
      //   },
      //   immediate: true,
      //   deep: true,
      // },
      bloodMatchTypePid() {
        if (this.activeName === 'second') {
          this.fetchData()
        }
      },
      activeName(newVal) {
        if (newVal === 'second') {
          this.fetchData()
        }
      },
    },
    created() {
      this.queryData()
    },
    methods: {
      getDate(value) {
        if (value) {
          return value.substr(0, value.length - 3)
        }
        return ''
      },
      tableRowClassName({ row }) {
        return getMatchTransClass(row)
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(removeEmptyField(this.queryForm))
        this.list = filterList(data, this.bloodMatchTypePid)
        this.list = this.mergeInvalidStatus(this.list)
        this.listLoading = false
      },
      mergeInvalidStatus(list) {
        list.forEach((item) => {
          item.invalidable = getMatchTransClass(item) === 'color-danger' ? 1 : 0
        })
        list = arraySort(list, 'invalidable')
        console.log(list)
        return list
      },
      handleInvalid() {
        if (this.selectRows.length > 0) {
          const rows = this.selectRows.map((val) => {
            return {
              BLOOD_MATCH_NEED_CANCEL: {
                MATCH_SHEET_NUM: val.MATCH_SHEET_NUM,
                BLOOD_ID: val.BLOOD_ID,
              },
            }
          })
          const result = {
            ArrayOfBLOOD_MATCH_NEED_CANCEL: rows,
          }

          invalidBloodMatches(result).then((res) => {
            this.$baseMessage(res.msg, 'success', 'vab-hey-message-success')
            this.fetchData()
          })
        } else {
          this.$baseMessage('请选择一条记录', 'error', 'vab-hey-message-error')
        }
      },
      handleRefresh() {
        this.fetchData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .pr8 {
    padding-right: 8px;
  }
</style>
